package mx.edu.itsur.sism.webapp.action;

import java.io.Serializable;

import mx.edu.itsur.sism.model.Maquina;
import mx.edu.itsur.sism.webapp.action.BasePage;
import mx.edu.itsur.sism.service.GenericManager;

public class MaquinaForm extends BasePage implements Serializable {
    private GenericManager<Maquina, Long> maquinaManager;
    private Maquina maquina = new Maquina();
    private Long id;

    public void setMaquinaManager(GenericManager<Maquina, Long> maquinaManager) {
        this.maquinaManager = maquinaManager;
    }

    public Maquina getMaquina() {
        return maquina;
    }

    public void setMaquina(Maquina maquina) {
        this.maquina = maquina;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        maquinaManager.remove(maquina.getId());
        addMessage("maquina.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            maquina = maquinaManager.get(id);
        } else {
            maquina = new Maquina();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (maquina.getId() == null || maquina.getId() == 0);
        maquinaManager.save(maquina);

        String key = (isNew) ? "maquina.added" : "maquina.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 